Parse = require('Parse')

local function testParsing()
    local status, p = pcall(Parse,
            "leader<Table foo=2>body</table>trailer", {"table"})
    if not status then
        if type(p) == "table" then
            print(p.message)
            print("Offset:",p.offset)
        else
            print(p)
        end
        return
    end
    assert(p.leader == "leader", "p.leader != \"leader\"\np.leader = " ..
        p.leader)
    assert(p.tag == "<Table foo=2>",
        "p.tag != \"<Table foo=2>\"\np.tag = " .. p.tag)
    assert(p.body == "body", "p.body != \"body\"\np.body = " ..
        p.body)
    assert(p.trailer == "trailer",
        "p.trailer != \"trailer\"\np.trailer = " .. p.trailer)
end

local function testRecursing()
    local p = Parse('leader<table><TR><Td>body</tD></TR></table>trailer')

    assert(p.body == nil, type(p.body))
    assert(p.parts.body == nil)
    assert(p.parts.parts.body == "body")
end

local function testIterating()
    local p = Parse("leader<table><tr><td>one</td><td>two</td><td>three</td></tr></table>trailer")

    assert(p.parts.parts.body == "one")
    assert(p.parts.parts.more.body == "two")
    assert(p.parts.parts.more.more.body == "three")
end

print("Calling testParsing()")
testParsing()
print("Calling testRecursing()")
testRecursing()
print("Calling testIterating()")
testIterating()
